package com.gongye.courseselectionsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gongye.courseselectionsystem.common.Check;
import com.gongye.courseselectionsystem.common.TimeJudge;
import com.gongye.courseselectionsystem.constants.ResultCodeEnum;
import com.gongye.courseselectionsystem.mapper.ChooseCourseMapper;
import com.gongye.courseselectionsystem.pojo.po.ChooseCourse;
import com.gongye.courseselectionsystem.pojo.po.Course;
import com.gongye.courseselectionsystem.pojo.po.Student;
import com.gongye.courseselectionsystem.pojo.vo.ResultList;
import com.gongye.courseselectionsystem.service.UpdateCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author baorongchuan
 */
@Service
@Slf4j
public class UpdateCourseServiceImpl implements UpdateCourseService {
    @Resource
    private ChooseCourseMapper chooseCourseMapper;
    @Resource
    private Check check;

    @Override
    public ResultList updateCourse(Integer studentNumber, Integer oldTeacherJobNumber, Integer newTeacherJobNumber, Integer newCourseNumber) {
        if (!TimeJudge.time()) {
            log.info("您不在选课时间内");
            return new ResultList(ResultCodeEnum.FAILED, "您不在选课时间内");
        }
        //判断学号，工号是否存在
        try {
            check.isExistNum(studentNumber, oldTeacherJobNumber);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultList(ResultCodeEnum.FAILED, e.getMessage());
        }
        //不能选择同一门课
        ChooseCourse course = check.isSameCourse(studentNumber, newCourseNumber);
        //不能选择同一个老师
        ChooseCourse course1 = check.haveTeacher(studentNumber, newTeacherJobNumber);
        if (!oldTeacherJobNumber.equals(newTeacherJobNumber)) {
            if (course1 != null) {
                return new ResultList(ResultCodeEnum.FAILED, "您不能选择同一个老师");
            }
            if (course != null) {
                return new ResultList(ResultCodeEnum.FAILED, "您不能选择同一门课");
            }
        }
        Course courseServiceCourseById;
        try {
            courseServiceCourseById = check.getCourseById(newCourseNumber, newTeacherJobNumber);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultList(ResultCodeEnum.FAILED, e.getMessage());
        }
        //判断老师的学生是否以达到三个
        boolean isThree = check.haveThree(newTeacherJobNumber, newCourseNumber);
        if (isThree) {
            log.info("该老师的课程已经被选满，请选择请他课程");
            return new ResultList(ResultCodeEnum.FAILED, "该老师的课程已经被选满，请选择请他课程");
        }

        Student student = check.getStudentById(studentNumber);
        String studentName = student.getName();
        String courseName = courseServiceCourseById.getCourseName();
        ChooseCourse chooseCourse = new ChooseCourse();
        chooseCourse.setCourseNumber(newCourseNumber);
        chooseCourse.setCourseName(courseName);
        chooseCourse.setTeacherJobNumber(newTeacherJobNumber);
        chooseCourse.setUpdateName(studentName);
        LambdaQueryWrapper<ChooseCourse> chooseCourseWrapper = new LambdaQueryWrapper<>();
        chooseCourseWrapper.eq(ChooseCourse::getStudentNumber, studentNumber);
        chooseCourseWrapper.eq(ChooseCourse::getTeacherJobNumber, oldTeacherJobNumber);
        int update = chooseCourseMapper.update(chooseCourse, chooseCourseWrapper);
        if (update == 0) {
            return new ResultList(ResultCodeEnum.FAILED, "请确认课程是否有对应的老师或者您选择了同一个课程或同一个老师");
        }
        return new ResultList(ResultCodeEnum.FAILED, "修改成功");
    }
}
